var token = localStorage.getItem("pid");
var list = document.getElementById("list");
var kucun = document.getElementById("kucun");
var feikucun = document.getElementById("feikucun");

var sidList = document.getElementById("sidList");
var warehouseList = document.getElementById("warehouseList");
var statusStr = "";

var pageNum = 1;
var itemNum = 20;
var countNum = 0;

var itemID = "";
var itemCode = "";
var funName = "";

var pageflag = "";
var sid = "";
var warehouse = "";
var itemID = "";
var type = ""
var funName = "";
var isShishi = false;

(function(e) {

	setPxPerRem();
	mui.plusReady(function() {
		itemID = getQueryString("itemID");
		itemCode = getQueryString("itemCode");
		funName = getQueryString("funName");
		sid = getQueryString("sid");
		isShishi = getQueryString("isShishi");
		statusStr = getQueryString("statusStr");
		warehouse = getQueryString("warehouse");
		type = getQueryString("type");
		pageflag = getQueryString("pageflag");

		if(pageflag == "page_password.html") {
			$("#type").html(type);
			if(type == "出库") {
				funName = "PhoneGetCKSub";
				$("#projectTitle").html("出库明细");
				$("#plan").html("计划出库");
				$("#actual").html("实际出库");
				$("#status").html("出库状态");
				$("#type1").html("订单号");
				$("#type2").html("船名");
			} else {
				$("#projectTitle").html("入库明细");
				$("#plan").html("计划入库");
				$("#actual").html("实际入库");
				$("#status").html("入库状态");
				$("#type1").html("发票号");
				$("#type2").html("提单号");
				
				funName = "PhoneGetRKSub";
			}

			hiddenEle(kucun);
			showEle(feikucun);
			if(!isEmpty(itemID)) {
				$("#code").html(itemID);
			}
			if(isHaveNet()) {
				getWarehouseInInfo(itemID);
			} else {
				mui.toast("请检查网络连接");
			}
		} else {
			if(isHaveNet()) {
				getWarehouse();
			} else {
				mui.toast("无网络连接！");
			}
			showEle(kucun);
			hiddenEle(feikucun);
		}

	});
})(mui);

var isRefresh = false;

function pulldownRefresh() {
	removeLi("list");
	isRefresh = true;
	pageNum = 1;
	isFirst = true;
	if(isHaveNet()) {
		getWarehouse();
	} else {
		mui.toast("无网络连接！");
	}
}

//上拉加载
var isLoad = false;
var isFirst = true;

function pullupRefresh() {
	isLoad = true;
	if(isHaveNet()) {
		getWarehouse();
	} else {
		mui.toast("无网络连接！");
	}
}

var count = 0;

function getWarehouseInInfo(itemId) {
	//	token = localStorage.getItem("token");
	//	if(isEmpty(token)) {
	//		mui.toast("登录认证失败，请退出后重试");
	//		return;
	//	}
	var pjson = {
		code: itemId
	};

	var jsonData = {
		op: funName,
		pjson: JSON.stringify(pjson)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		if(!isEmpty(data)) {
			$("#Code").html(itemId);
			var rows = data;
			count = 0;
			for(var i = 0; i < rows.length; i++) {
				createItem(rows[i], i);
			}
			$("#countNum").html(count);
		} else {
			mui.toast("暂无数据");
		}
	});
}

var keyword = "";
var otherSid = "";
var warehouse_id = "";

function getWarehouse() {
	//	token = localStorage.getItem("token");
	//	if(isEmpty(token)) {
	//		mui.toast("登录认证失败，请退出后重试");
	//		return;
	//	}
	plus.nativeUI.showWaiting();
	if(isShishi && isGet) {
		if(isEmpty(currentSid) || isEmpty(currentWarehouse)) {
			mui.toast("请选择仓库后在查询");
			return;
		}
	}

	var pjson = {
		pid: token,
		"code": keyword
	};

	//	if(isShishi) {
	//		pjson = {
	//			"op": "warehouse_stock_rt",
	//			"token": token,
	//			"data": {
	//				"sid": currentSid,
	//				"warehouse_id": currentWarehouse,
	//				"keyword": keyword
	//			}
	//		};
	//	}

	var jsonData = {
		op: "GetStoreStock",
		pjson: JSON.stringify(pjson)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {
		//		if(!isGet) {
		//			getSidList();
		//		} else {
		//		}
		plus.nativeUI.closeWaiting();
		if(!isEmpty(data)) {
			var rows = data;
			count = 0;
			for(var i = 0; i < rows.length; i++) {
				createItem(rows[i], i);
			}
			$("#countNum").html(count);
		} else {
			mui.toast("暂无数据");
		}
	});
}

$("#queryBtn").click(function() {
	keyword = $("#searchInput").val().trim();
	if(isEmpty(keyword) && !isShishi) {
		mui.toast("请输入入库单号查询");
		return;
	}
	getWarehouse();
});

var insertDiv = document.getElementById("insertDiv");

function createItem(row, index) {
	var createLi = document.createElement("li");
	createLi.id = row.id;

	var secondCountStr = row.KC_SecondCount;
	var acturlNum = row.SS_SecondCount;
	var id = index + 1;
	var SM_Name = row.sm_name;
	var SM_Code = row.sm_nbcode;
	var SM_Spec = row.sm_spec;
	var SP_Place = row.SS_SPPlace;

	if(pageflag == "page_password.html") {
		if(type == "出库") {
			if(index == 0){
				$("#code2").html(row.SOM_ShipName);
				$("#code1").html(row.SOM_TiDanCode);
			}
			secondCountStr = row.SOS_SecondCount;
		} else {
			if(index == 0){
				$("#code1").html(row.SIM_InvoiceNo);
				$("#code2").html(row.SIM_TiDanCode);
			}
			secondCountStr = row.SIS_SecondCount;
		}
	}
	count = Math.round(secondCountStr) + count;
	var secondCount = secondCountStr;

	createLi.innerHTML = '<cite style="width: 1.6rem;">' + id + '</cite>' +
		'<cite style="width: 2.6rem;">' + SM_Name + '</cite>' +
		'<cite style="width: 3.6rem;">' + SM_Code + '</cite>' +
		'<cite style="width: 2.6rem;">' + SM_Spec + '</cite>' +
		'<cite style="width: 2.1rem;">'+ itemCode +'</cite>'+
		'<cite style="width: 2.6rem;">' + secondCount + '</cite>' +
		'<cite style="width: 2.6rem;">' + acturlNum + '</cite>';
	document.body.appendChild(createLi);
	insertBefore(createLi, insertDiv);
}

var sidEle = document.getElementById("sid");
var warehouseEle = document.getElementById("warehouse");
var isGet = false;

function getSidList() {
	//	token = localStorage.getItem("token");
	var p = {
		op: "provider_list",
		token: token
	}
	var jsonData = {
		p: JSON.stringify(p)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		isGet = true;
		var htmlStr = '<option value="-1" selected>请选择供应商</option>';
		if(!isEmpty(data)) {
			for(var i = 0; i < data.length; i++) {
				htmlStr += '<option value="' + data[i].o_tid + '">' + data[i].o_name + '</option>'
			}
		}
		sidEle.innerHTML = htmlStr;
	});
}

var currentSid = "";
var currentWarehouse = "";
var isLoad = false;

function getWarehouseList() {
	var p = {
		op: "warehouse_list",
		token: token,
		data: {
			sid: currentSid,
			keyword: ""
		}
	}
	var jsonData = {
		p: JSON.stringify(p)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}
	plus.nativeUI.showWaiting();
	crossDomainAjax(opts, function(data) {
		isLoad = true;
		plus.nativeUI.closeWaiting();
		var htmlStr = '<option value="-1" selected="selected">请选择仓库</option>';
		if(!isEmpty(data)) {
			for(var i = 0; i < data.length; i++) {
				htmlStr += '<option value="' + data[i].id + '" >' + data[i].S_Name + '</option>'
			}
		}
		warehouseEle.innerHTML = htmlStr;
	});
}

sidEle.addEventListener('change', function() {
	currentSid = this.value;
	if(currentSid == "-1" || isEmpty(currentSid)) {
		mui.toast("请先选择供销商");
		return;
	}
	warehouseEle.innerHTML = '<option value="-1" selected="selected">请选择仓库</option>';
	getWarehouseList();

}, false);

warehouseEle.addEventListener('change', function() {
	currentWarehouse = this.value;
}, false);